package com.flyme.module.base.provider.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.module.base.client.api.SysWorkCourseRemoteService;
import com.flyme.module.base.client.entity.SysWorkCourse;
import com.flyme.module.base.client.entity.Sysenum;
import com.flyme.module.base.client.enums.PostEnum;
import com.flyme.module.base.client.enums.WorkingYearEnum;
import com.flyme.module.base.provider.service.*;
import com.flyme.module.exam.client.entity.BusUserability;
import com.flyme.module.exam.client.entity.ExamType;
import com.flyme.module.exam.client.enums.AbilityItemEnum;
import com.flyme.module.exam.provider.service.BusUserabilityService;
import com.flyme.module.exam.provider.service.ExamTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 工作经历
 *
 * @author zyf
 */
@Api(tags = "工作经历管理")
@RestController
public class SysWorkCourseController extends BaseController<SysWorkCourseService, SysWorkCourse> implements SysWorkCourseRemoteService {

    @Autowired
    private SysConfigService configService;
    @Autowired
    private SysenumService sysenumService;
    @Autowired
    private SysCompanyService companyService;
    @Autowired
    private ExamTypeService examTypeService;
    @Autowired
    private BusUserabilityService userabilityService;
    @Autowired
    private SysCompanytopService companytopService;

    /**
     * 查询用户工作经历
     *
     * @return
     */
    @ApiOperation(value = "查询用户工作经历")
    @Override
    public ResultBody selectByUserId(@PathVariable Long userId) {
        List<SysWorkCourse> list = bizService.selectByUserId(userId);
        for (SysWorkCourse work : list) {
            work.setPosition(PostEnum.getText(work.getPosition()));
        }
        return ResultBody.success(list);
    }

    /**
     * 认证用户工作经历
     */
    @ApiOperation(value = "认证用户工作经历")
    public ResultBody checkUserWork(@PathVariable String id) {
        UpdateWrapper u = new UpdateWrapper();
        boolean a = bizService.update(u.set(true, "isWork", 1).eq(true, "id", id));
        if(a){
            gradeWork(bizService.getById(id));
        }
        return ResultBody.success();
    }


    /**
     * 拒绝认证用户工作经历
     */
    @ApiOperation(value = "拒绝认证用户工作经历")
    public ResultBody checkUserWorkRefuse(@PathVariable String id) {
        UpdateWrapper u = new UpdateWrapper();
        boolean a = bizService.update(u.set(true, "isWork", 3).eq(true, "id", id));
        return ResultBody.success();
    }

    /**
     * 工作经历打分
     */
    private void gradeWork(SysWorkCourse wc) {
        WorkingYearEnum workingYearEnum = wc.getWorkingLife();
        Long userId = wc.getUserId();
        if (!WorkingYearEnum.sixMonth.code.equals(workingYearEnum.code)) {
            Sysenum comTop = sysenumService.findByCode("comTop");//百强企业系数分
            Sysenum comGeneral = sysenumService.findByCode("comGeneral");//非百强企业系数分
            BigDecimal basics = new BigDecimal(configService.getByName("WORK").getConfigVal());//基础分
            List<SysWorkCourse> list = bizService.selectWork(userId);
            BigDecimal allScore = new BigDecimal("0");
            for (SysWorkCourse workcourse : list) {
                //工作年限不满半年、且未认证的工作经历
                if (WorkingYearEnum.sixMonth.code.equals(workcourse.getWorkingLife())) {
                    continue;
                }
                Sysenum positionEnum = sysenumService.findByCode(workcourse.getPosition());//职位
                Sysenum workingLifeEnum = sysenumService.findByCode(workcourse.getWorkingLife().code);//工作年限
                List<Map<String, Object>> comList = companytopService.selectByName(workcourse.getCompanyName());
                BigDecimal company = new BigDecimal(comGeneral.getScore());//非百强
                if (comList.size() > 0) {
                    company = new BigDecimal(comTop.getScore());//百强
                }
                BigDecimal a = new BigDecimal(positionEnum.getScore());
                BigDecimal b = new BigDecimal(workingLifeEnum.getScore());
                BigDecimal num = new BigDecimal(list.size());
                BigDecimal score = basics.multiply(a.multiply(company)).divide(num, 2, BigDecimal.ROUND_HALF_UP).multiply(b);
                allScore = allScore.add(score);
            }
            ExamType examType = examTypeService.findByCode(AbilityItemEnum.work.code);
            BusUserability userability = userabilityService.findUser(userId, examType.getId());
            if (ObjectUtils.isNotEmpty(userability)) {
                userability.setAbilityScore(String.valueOf(allScore));
                userabilityService.saveOrUpdate(userability);
            }
        }
    }
}
